import java.util.*;
import java.io.*;

public class Database {
    /** 保存用户名和密码的哈希表 */
    private HashMap<String, AccountPassword> AccountPasswordDataMap = new HashMap<>();
    /** 保存用户名和战绩的哈希表 */
    private HashMap<String, AccountRecord> AccountRecordDataMap = new HashMap<>();

    public Map<String, AccountRecord> getAccountRecordDataMap() {
        return AccountRecordDataMap;
    }

    public Map<String, AccountPassword> getAccountPasswordDataMap() {
        return AccountPasswordDataMap;
    }

    /** 通过用户名查询密码 */
    public AccountPassword getAccountPassword(String account) {
        return AccountPasswordDataMap.containsKey(account) ? AccountPasswordDataMap.get(account) : null;
    }

    /** 通过用户名查询战绩 */
    public AccountRecord getAccountRecord(String account) {
        return AccountRecordDataMap.containsKey(account) ? AccountRecordDataMap.get(account) : null;
    }

    /** 通过用户名插入密码 */
    public void putAccountPassword(String account, AccountPassword password) {
        AccountPasswordDataMap.put(account, password);
    }

    /** 通过用户名插入密码 */
    public void putAccountPassword(String account, String password) {
        AccountPasswordDataMap.put(account, new AccountPassword(password));
    }

    /** 通过用户名插入战绩 */
    public void putAccountRecord(String account, AccountRecord record) {
        AccountRecordDataMap.put(account, record);
    }

    /** 从文件读入数据 */
    public void readFromFile() throws FileNotFoundException {
        try (Scanner scanner = new Scanner(new File(".\\AccountPasswordData.txt"));) {
            while (scanner.hasNextLine()) {
                String input = scanner.nextLine();
                String account = input.split("\t", 2)[0];
                String password = input.split("\t", 2)[1];
                putAccountPassword(account, password);
            }
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException();
        }
        try (Scanner scanner = new Scanner(new File(".\\AccountRecordData.txt"));) {
            while (scanner.hasNextLine()) {
                String input = scanner.nextLine();
                String[] split = input.split("\t", 4);
                String account = split[0];
                int money = Integer.parseInt(split[1]);
                int win = Integer.parseInt(split[2]);
                int games = Integer.parseInt(split[3]);
                putAccountRecord(account, new AccountRecord(money, win, games));
            }
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException();
        }
    }

    /** 将数据写出文件 */
    public void writeToFile() throws IOException {
        try (BufferedWriter out = new BufferedWriter(new FileWriter(".\\AccountPasswordData.txt"))) {
            Set<? extends String> s1 = AccountPasswordDataMap.keySet();
            Collection<? extends AccountPassword> c1 = AccountPasswordDataMap.values();
            Iterator<? extends String> itr1 = s1.iterator();
            Iterator<? extends AccountPassword> itr2 = c1.iterator();
            while (itr1.hasNext()) {
                out.write(itr1.next().toString() + "\t");
                out.write(((AccountPassword) itr2.next()).getPassword() + "\n");
            }
        }
        try (BufferedWriter out = new BufferedWriter(new FileWriter(".\\AccountRecordData.txt"))) {
            Set<? extends String> s1 = AccountRecordDataMap.keySet();
            Collection<? extends AccountRecord> c1 = AccountRecordDataMap.values();
            Iterator<? extends String> itr1 = s1.iterator();
            Iterator<? extends AccountRecord> itr2 = c1.iterator();
            while (itr1.hasNext()) {
                out.write(itr1.next().toString() + "\t");
                AccountRecord temp = itr2.next();
                out.write(temp.getMoney() + "\t");
                out.write(temp.getWin() + "\t");
                out.write(temp.getGames() + "\n");
            }
        }
    }
}
